package com.mate.cloud.controller;


import cn.hutool.json.JSONUtil;
import com.mate.cloud.common.config.PropertyConfig;
import com.mate.cloud.common.response.BaseResponse;
import com.mate.cloud.common.web.controller.AdminBaseController;
import com.mate.cloud.entity.User;
import com.mate.cloud.exception.BusinessException;
import com.mate.cloud.exception.UserExceptionCode;
import com.mate.cloud.service.ExceptionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 国际化实战
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createDate: 2023/10/29 18:19
 * @updateUser: MI
 * @updateDate: 2023/10/29 18:19
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("/valid")
public class MessageController extends AdminBaseController {

    @Resource
    private ExceptionService exceptionService;

    @PostMapping("/save")
    public BaseResponse save(@Validated @RequestBody User user) {
        log.info("==========MessageController#save==========", JSONUtil.toJsonStr(user));
        return successBodyResponse(user);
    }

    @GetMapping("/i18n")
    public String i18n() {
        return PropertyConfig.getProperty(UserExceptionCode.PARAMS_ERROR.getCode());
    }


    @GetMapping("/load")
    public BaseResponse load() {
        throw new BusinessException(UserExceptionCode.EMAIL_ALREADY_REGISTER.getCode(), "li448341911@qq.com");
    }

    @GetMapping("/parameters")
    public BaseResponse parameters() {
        throw new BusinessException(UserExceptionCode.EXPORT_SUCCESS.getCode(), "li448341911@qq.com","https://dmxixi.com/p/8-1-95.html");
    }

    @GetMapping("/export")
    public BaseResponse export() {
        throw new BusinessException(UserExceptionCode.EXPORT_FAIL.getCode(), "120","2");
    }

    @GetMapping("/error")
    public BaseResponse error(@RequestParam("code") String code) {
        exceptionService.exceptionMessage(code);
        return successCodeResponse();

    }

    @GetMapping("/exception")
    public BaseResponse exception() {
        exceptionService.exceptionMessage();
        return successCodeResponse();
    }

    /**
     * 运行时异常（占位符替换演示）
     * @return
     */
    @GetMapping("/message")
    public BaseResponse message() {
        exceptionService.exceptionCodeMessage();
        return successCodeResponse();
    }
}
